package com.cgsd.heyu.ui.info;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.library.utils.BeanUtils;
import com.cgsd.heyu.R;
import com.cgsd.heyu.adapter.MyTeamAdapter;
import com.cgsd.heyu.entity.TeamEntity;
import com.cgsd.heyu.entity.VipEntity;
import com.cgsd.heyu.presenter.home.HomeContract;
import com.cgsd.heyu.presenter.home.HomePresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.widgets.ErrorCustomView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxing
 * Description 我的团队
 * CreateData: 2021/1/7
 */
public class MyTeamActivity extends BaseActivity<HomeContract.Presenter> implements HomeContract.MyTeamView, OnLoadMoreListener {
    private MyTeamAdapter adapter;
    private List<VipEntity> data = new ArrayList<>();
    private SmartRefreshLayout mSmartRefreshLayout;
    private int pageSize = 20;
    private TextView mTvRecommendNum1;
    private TextView mTvRecommendNum2;
    private TextView mTvIndirectRecommendNum1;
    private TextView mTvIndirectRecommendNum2;
    private TextView mTvTotalRecommend;
    private TextView mTvTotalIndirect;
    private ErrorCustomView errorCustomView;

    private View mViewDirect;
    private View mViewIndirect;
    private boolean isDirect = true; //直接推荐


    public static void actionStart(Activity activity){
        Intent intent = new Intent(activity,MyTeamActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_team;
    }

    @Override
    protected void initView() {
        new HomePresenter(this);
        mTvRecommendNum1 = findViewById(R.id.tv_recommendNum1);
        mTvRecommendNum2 = findViewById(R.id.tv_recommendNum2);
        mTvIndirectRecommendNum1 = findViewById(R.id.tv_indirectRecommendNum1);
        mTvIndirectRecommendNum2 = findViewById(R.id.tv_indirectRecommendNum2);
        mTvTotalRecommend = findViewById(R.id.tv_total_recommend);
        mTvTotalIndirect = findViewById(R.id.tv_total_indirect);

        mSmartRefreshLayout = findViewById(R.id.mSmartRefreshLayout);
        errorCustomView = findViewById(R.id.mErrorView);
        mSmartRefreshLayout.setEnableRefresh(false);

        RecyclerView mRecyclerView = findViewById(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyTeamAdapter(this,data);
        mRecyclerView.setAdapter(adapter);

        mSmartRefreshLayout.setOnLoadMoreListener(this);

        mViewDirect = findViewById(R.id.view_direct);
        mViewIndirect = findViewById(R.id.view_indirect);

        findViewById(R.id.layout_direct).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isDirect){
                    isDirect = true;
                    page = 1;
                    data.clear();
                    mViewDirect.setVisibility(View.VISIBLE);
                    mViewIndirect.setVisibility(View.INVISIBLE);
                    mSmartRefreshLayout.setEnableLoadMore(false);
                    getList();
                }
            }
        });

        findViewById(R.id.layout_indirect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isDirect){
                    isDirect = false;
                    page = 1;
                    data.clear();
                    mViewDirect.setVisibility(View.INVISIBLE);
                    mViewIndirect.setVisibility(View.VISIBLE);
                    mSmartRefreshLayout.setEnableLoadMore(false);
                    getList();
                }
            }
        });


    }

    @Override
    protected void initData() {
        mPresenter.getTeamData();
        getList();
    }


    @Override
    public void setTeamData(TeamEntity entity) {

        if (entity != null){
            mTvRecommendNum1.setText(entity.recommendNum);
            mTvRecommendNum2.setText(entity.recommendNumVip);
            mTvIndirectRecommendNum1.setText(entity.indirectRecommendNum);
            mTvIndirectRecommendNum2.setText(entity.indirectRecommendNumVip);
            mTvTotalRecommend.setText(entity.teamNum);
            mTvTotalIndirect.setText(entity.teamNumVip);
        }
    }

    @Override
    public void vipList(List<VipEntity> entity) {
        dismissCommonSubmitDialog();
        mSmartRefreshLayout.finishLoadMore();
        if (BeanUtils.isNotEmpty(entity)){
            int index = data.size();
            data.addAll(entity);
           // adapter.notifyItemRangeChanged(index,entity.size());

            if (entity.size() < pageSize) {
                mSmartRefreshLayout.setEnableLoadMore(false);
            } else {
                mSmartRefreshLayout.setEnableLoadMore(true);
            }
        }else {
            mSmartRefreshLayout.setEnableLoadMore(false);
        }
        if (data.size() == 0){
            showErrorView();
        }else {
            errorCustomView.setVisibility(View.GONE);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        getList();
    }

    /**
     * 数据为null
     */
    private void showErrorView(){
        errorCustomView.showNetworkError(R.mipmap.default_no_data,"暂无直推详情～");
        errorCustomView.setVisibility(View.VISIBLE);
    }

    private void getList(){
        showCommonSubmitDialog("");
        if (isDirect) {
            mPresenter.recommendUserList(page, pageSize, 1);
        } else {
            mPresenter.recommendUserList(page, pageSize, 2);
        }
    }


}
